﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Taula = DeltaFlex.Negoci.ConsTaules.Factura;

namespace DeltaFlex.Negoci
{
    [Serializable()]
    public class Factura
    {
        #region Variables
        private int _idFactura;
        private Client _client;
        private DateTime _data;
        private DateTime _venciment;
        private decimal _totalFactura;
        #endregion

        #region Atributs
        public int IdFactura
        {
            get { return _idFactura; }
        }

        public Client Client
        {
            get { return _client; }
            set { _client = value; }
        }

        public DateTime Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public DateTime Venciment
        {
            get { return _venciment; }
            set { _venciment = value; }
        }


        public decimal TotalFactura
        {
            get { return _totalFactura; }
            set { _totalFactura = value; }
        }
        #endregion

        #region Inserts
        public static void InsertarFactura(int idFactura, int idClient, DateTime data, DateTime venciment, decimal total)
        {
            try
            {
                DeltaFlex.Data.Factura.InsertarFactura(idFactura, idClient, data, venciment, total);
            }
            catch { throw; }
        }
        #endregion

        #region Updates
        #endregion

        #region Selects 
        public static int Cercar_NextIdFactura()
        {
            int nextIdFactura = -1;
            IDataReader reader = null;

            try
            {
                reader = DeltaFlex.Data.Factura.Cercar_NextIdFactura();

                if (reader.Read())
                    nextIdFactura = MapEntityFromDataReader(reader)._idFactura;
            }
            catch (Exception) { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }
            return nextIdFactura;
        }
        #endregion

        #region Mapeig
        public static Factura MapEntityFromDataReader(IDataReader record)
        {
            Factura factura = new Factura();

            MemberAllocation.Allocate(record, Taula.COLUMNA_IDFACTURA, ref factura._idFactura);
            factura._client = Client.MapEntityFromId(record);
            MemberAllocation.Allocate(record, Taula.COLUMNA_DATA, ref factura._data);
            MemberAllocation.Allocate(record, Taula.COLUMNA_VENCIMENT, ref factura._venciment);
            MemberAllocation.Allocate(record, Taula.COLUMNA_TOTAL, ref factura._totalFactura);

            return factura;

        }
        #endregion
    }
}
